﻿<%@ WebHandler Language="C#" Class="Jcrop" %>

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

public class Jcrop : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";

        string img_x = context.Request["x"].ToString();
        string img_y = context.Request["y"].ToString();
        string img_w = context.Request["w"].ToString();
        string img_h = context.Request["h"].ToString();
        String imgUrl = context.Request["imgUrl"].ToString();

        Bitmap bm = new Bitmap(context.Server.MapPath(imgUrl));
        Rectangle cloneRect = new Rectangle(int.Parse(img_x), int.Parse(img_y), int.Parse(img_w), int.Parse(img_h));
        PixelFormat format = bm.PixelFormat;
        Bitmap cloneBitmap = bm.Clone(cloneRect, format);

        string imgName = DateTime.Now.ToString("yyyyMMddHHmmss") + "a.jpg"; //给截取的图片取名
        string path = context.Server.MapPath("pic/") + imgName;                     //获取路径
        cloneBitmap.Save(path, ImageFormat.Jpeg);                           //存放图片

        context.Response.Write(imgName);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}